//
//  ProfileManager.m
//  SDKUI
//
//  Created by aiquantong on 18/08/2017.
//  Copyright © 2017 aiquantong. All rights reserved.
//

#import "ProvisionManager.h"

#import "ProfileManager.h"
#import "SessionMangementManager.h"

#import "CacheUtil.h"


@implementation ProfileManager


static ProfileManager *shareInstance = nil;

+(ProfileManager *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (shareInstance == nil) {
            shareInstance = [[ProfileManager alloc] init];
        }
    });
    return shareInstance;
}


+(void)getCurUserProfile:(KandyProfileCallback)callback
{
    [[Kandy sharedInstance].services.profile
     getProfileWithResponseCallback:^(NSError *error, KandyUserProfile *userProfile) {
         KDALog(@"error == %@ userProfile == %@ ", [error description], [userProfile description]);
         if(callback){
             callback(error,userProfile);
         }
     }];
}


+(void)updateUserProfileName:(NSString *)name callback:(KandyCallback)callback
{
    [[Kandy sharedInstance].services.profile updateUserName:name responseCallback:^(NSError *error, KandyUserProfile *userProfile) {
        KDALog(@"error == %@ userProfile == %@ ", [error description], [userProfile description]);
        if (callback) {
            callback(error);
        }
    }];
}

+(void)updateUserProfileImage:(NSString *)image callback:(KandyCallback)callback
{
    [[Kandy sharedInstance].services.profile updateProfileImage:image
                                               progressCallback:^(KandyTransferProgress *transferProgress) {
                                               }
                                               responseCallback:^(NSError *error, KandyUserProfile *userProfile) {
                                                   KDALog(@"error == %@ userProfile == %@ ", [error description], [userProfile description]);
                                                   if (callback) {
                                                       callback(error);
                                                   }
                                               }];
}


+(void)downloadUserProfileImage:(NSString *)imageuuid callback:(KandyPathCallback)callback;
{
    NSString *cachRootPath = [CacheUtil getCacheRootPath:[SessionMangementManager getcurLoginUser]];
    NSString *cachImagePath = [NSString stringWithFormat:@"%@/%@.png",cachRootPath, imageuuid];
    
    if ([[NSFileManager defaultManager]fileExistsAtPath:cachImagePath]) {
        if (callback) {
            callback(nil, cachImagePath);
        }
    }
    
    id<KandyFileItemProtocol> fileItem = [KandyMessageBuilder createFileItem:cachImagePath text:nil];
    [fileItem updateContentUUID:imageuuid];
    
    [[Kandy sharedInstance].services.cloudStorage
     downloadMedia:fileItem fileName:imageuuid
     downloadPath:cachRootPath
     progressCallback:^(KandyTransferProgress *transferProgress) {
         
     } responseCallback:^(NSError *error, NSString *filePath) {
         if (callback) {
             callback(error, filePath);
         }
     }];
}


+(void)getUserProfileWithUserId:(NSString *)userId callback:(KandyProfileCallback)callback
{
    KandyRecord *kr = [[KandyRecord alloc] initWithURI:userId type:EKandyRecordType_contact];
    [[Kandy sharedInstance].services.profile
     getProfileByContactId:kr responseCallback:^(NSError *error, KandyUserProfile *userProfile) {
         KDALog(@"error == %@ userProfile == %@ ", [error description], [userProfile description]);
         if(callback){
             callback(error,userProfile);
         }
     }];
}


@end
